#!/usr/bin/env perl
# vmap2dot.pl: 

# vmap --> .dot

# Basile Clout, 
# November 2007


use strict;
use warnings;

sub usage() {
  print<<EOF;
Usage: $0 [-h] <vmap>
EOF

exit;
}


usage() if ($#ARGV != 0 or $ARGV[0] eq "-h");
open(FILE, "<$ARGV[0]") or die "Impossible to read vmap file $ARGV[0]: $!";

my $cnt=-1;
my $i=-1;
my $j;
my $host;
my $mod=-1;
my @parts;

while (my $line = <FILE>){

  if ($line =~ /^% pagrid/){
    $mod = 0;
    print "digraph vnetpagrid {\n";
    next;
  }elsif ($line =~ /^% (weights|values)/){
    $mod = 1;
    print "digraph vnet$1 {\n";
    next;
  }

  next if ($line =~ /^[%#]/);
  
  $cnt++;
  next if ($cnt<2 and $mod == 0);

  $i++;
  
  if ($mod == 1){
    @parts = split /\s+/,$line;
    my $loc = $parts[0];
    $loc =~ s/://; 		# or tr/://d
    print "\t$loc [label=\"$loc ($parts[1])\"];\n";
    $j=2;
    while ($j < $#parts){
      print "\t$loc -> $parts[$j++] [label=\"$parts[$j++]\\n$parts[$j++]\"];\n";
    }
  }elsif ($mod == 0){
    @parts = split /\s+/, $line;
    print "\t$i [label=\"$i ($parts[0])\"];\n";
    $j=1;
    while($j < $#parts){
      print "\t$i -> $parts[$j++] [label=\"$parts[$j++]\\n$parts[$j++]\"];\n";
    }
  }else{
    print "Bad vmap format: first line must start with \'% pagrid\', \'% weights\' or \'% values\'\n";
    exit;
  }
}

print "}\n";

